using System;
using System.Collections.Generic;
using Yoozoo.Core.Common;
using Yoozoo.Gameplay.RTS;
using Yoozoo.Gta.Common;
using Yoozoo.Mars.Got;
using Yoozoo.Mars.Got.SpriteAnim;

namespace com.youzu.got.protocol
{
    public class RTSQualityHelper
    {

        private static bool _SimpleMode;

        public static bool IsSimpleMode
        {
            set
            {
                _SimpleMode = value;
                OnSimpleModeChanged();
                OnCommanderHeadChange();
            }

            get
            {
                return _SimpleMode;
            }
        }

        public static int SelfEffectUsingCount;
        public static int OtherEffectUsingCount;

        public static int SelfEffectMaxCount;
        public static int OtherEffectMaxCount;
        private static int _Quality = 2;

        public static bool IsShowThirdAttackSpeedLineEffect = true;
        public static bool IsShowThirdAttackEffect = true;
        public static bool IsShowThirdRageEffect = true;
        public static bool IsShowThirdNormalText = true;
        public static bool IsShowThirdSkillText = true;

        public static bool _IsShowCommanderHead = true;

        public static int Quality {
            get
            {
                return _Quality;
            }
            set
            {
                _Quality = value;
                OnQualityChange();
                OnCommanderHeadChange();
            }        
        }
        public static bool IsShowCommanderHead
        {
            get
            {
                return _IsShowCommanderHead;
            }
            set
            {
                _IsShowCommanderHead = value;
                OnCommanderHeadChange();
            }
        }


        /// <summary>
        /// 池子初始化百分比数
        /// </summary>
        public static float PoolInitCountPercent = 1;

        public static int MaxFlyTextTroopNum = 100;
        public static int MaxAttackEffectTroopNum = 30;
        public static int MaxSkillTroopNum = 15;

        public static int MaxFlyTextNum = -1;
        public static int MaxAttackEffectNum = -1;
        public static int MaxSkillEffectNum = -1;
        public static bool LimitSelfTroop = false;
        public static int GetPoolInitNum(int poolNum)
        {
            return 1;
            return (int)(PoolInitCountPercent * poolNum);
        }

        public static void Initialize()
        {
            IsSimpleMode = false;
            SelfEffectUsingCount = 0;
            OtherEffectUsingCount = 0;
        }

        public static int SelfEffectCountLimited
        {
            get
            {
                if (IsSimpleMode)
                {
                    return 0;
                }
                return SelfEffectMaxCount;
            }
        }

        public static int OtherEffectCountLimited
        {
            get
            {
                if (IsSimpleMode)
                {
                    return 0;
                }
                return OtherEffectMaxCount;
            }
        }

        public static bool CheckIsEffectOverloaded(bool isRelatedToSelf)
        {
            if (isRelatedToSelf)
            {
                return SelfEffectUsingCount >= SelfEffectCountLimited;
            }
            return OtherEffectUsingCount >= OtherEffectCountLimited;
        }

        public static void ChangeSimpleMode(bool isSimpleMode,bool isAuto)
        {
            if (isSimpleMode != IsSimpleMode)
            {
                IsSimpleMode = isSimpleMode;
                if (isAuto)
                {
                    ResidentHandler.Inst.GetFunction("G_MarchManager.ChangeSimpleMode").Action(isSimpleMode);
                }
            }
        }

        public static bool ShowAttackSpeed()
        {
            return MarchManager.BattleTroopCount < MaxAttackEffectTroopNum;
        }

        public static bool ShowAttackEffect()
        {
            return MarchManager.BattleTroopCount < MaxAttackEffectTroopNum;
        }

        public static bool ShowOtherSkillEffect()
        {
            return MarchManager.BattleTroopCount < MaxSkillTroopNum;
        }

        private const int Level1 = 25;
        private const int Level2 = 50;

        private static int currentLevelId;
        private static int _numId;
        public static void OnMarchNumChanged(int num)
        {
            int baseId = 0;
            if (IsSimpleMode)
            {
                baseId = 10;
            }
            int numId;
            if (num < Level1)
            {
                numId = 1;
            }
            else if (num >= Level1 && num < Level2)
            {
                numId = 2;
            }
            else
            {
                numId = 3;
            }

            int id = baseId + numId;
            if (_numId != numId)
            {
                _numId = numId;
            }
            if (id != currentLevelId)
            {
                currentLevelId = id;
                OnQualityChange();
            }
        }

        private static void OnSimpleModeChanged()
        {
            int baseId = 0;
            if (IsSimpleMode)
            {
                baseId = 10;
            }
            int id = baseId + _numId;
            if (id != currentLevelId)
            {
                currentLevelId = id;
                OnQualityChange();
            }
        }

        private static void OnQualityChange()
        {
            if (ArmyManager.Instance!=null && ArmyManager.Instance.ResourceManager!=null)
            {
                var config = ArmyManager.Instance.ResourceManager.GetRTSQualityLevelConfig(currentLevelId, Quality);
                if (config != null)
                {
                    MaxFlyTextNum = config.MaxFlyTextNum;
                    MaxAttackEffectNum = config.MaxAttackEffectNum;
                    MaxSkillEffectNum = config.MaxSkillEffectNum;
                    LimitSelfTroop = config.IsLimitSelf;
                }
            }
            
        }

        private static void OnCommanderHeadChange()
        {
            ResidentHandler.Inst.GetFunction("G_MarchManager.onUpdateAllMarchHead").Action();
        }
    }
}
